import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

import axios from 'axios'
import VueAxios from 'vue-axios'
import VueRouter from 'vue-router'

// 导入vue-table-width-tree-grid树形表格组件
import TreeTable from 'vue-table-with-tree-grid'
Vue.component('tree-table', TreeTable);

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

// 引入vue-quill-editor文本编辑器
import VueQuillEditor from 'vue-quill-editor';
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
// 全局注册
Vue.use(VueQuillEditor);


import _ from 'lodash';
Vue.prototype._ = _;

// 导入需要登录才能访问得路由模块
import paths from './router/path';


Vue.use(VueAxios, axios)


// 设置请求基础路径
axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/';


// axios请求 响应之前拦截器
axios.interceptors.request.use(config => {
        // 

        // 为请求头对象，添加token验证的authorization字段
        config.headers.Authorization = window.sessionStorage.getItem('token');
        return config;
    },

    err => {
        return Promise.reject(err);
    }
)

// 添加一个响应之后拦截器
axios.interceptors.response.use(res => {
    return res;
}, err => {
    return Project.reject(err);
})



// 点击相同路由报错的解放方法
// 捕获错误
let originPush = VueRouter.prototype.push;

// 重新VueRouter的原型方法
VueRouter.prototype.push = function(location) {
    originPush.call(this, location, () => {}, (err) => {

    })
}

Vue.config.productionTip = false

// 路由前置守卫
router.beforeEach((to, from, next) => {
    if (to.meta.auth) {
        const token = sessionStorage.getItem('token');
        if (token) {
            next();
        } else {
            next('/login');
        }
    } else {
        next();
    }
})

//定义格式化时间过滤器
Vue.filter('formatDate', (value, format) => {
    //value: 日期对象 || 日期字符串
    //format: 格式 ==> 比如 yyyy-MM-dd hh:mm:ss
    //月日时分秒不足十则补零

    if (Object.prototype.toString.call(value) !== ['object Date']) {
        value = new Date(value * 1000);
    }

    //获取年份
    let yearReg = /(y+)/;

    if (yearReg.test(format)) {

        //获取匹配组的内容
        let yearContent = RegExp.$1;

        //获取年份
        let year = value.getFullYear().toString();
        let yearText = year.slice(4 - yearContent.length);

        format = format.replace(yearContent, yearText);
    }

    let dateObject = {
        M: value.getMonth() + 1,
        d: value.getDate(),
        h: value.getHours(),
        m: value.getMinutes(),
        s: value.getSeconds()
    };


    //遍历dateObject替换format
    for (let key in dateObject) {

        //动态创建正则表达式
        let reg = new RegExp(`(${key}+)`);

        if (reg.test(format)) {
            //获取匹配组的内容
            let content = RegExp.$1;
            format = format.replace(content, content.length === 1 ? dateObject[key] : dateObject[key] >= 10 ? dateObject[key] : `0${dateObject[key]}`);
        }

    }

    return format;

})

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')